package top.hepingan.java.service.Impl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import top.hepingan.java.entity.Muser;
import top.hepingan.java.entity.PhoneRegister;
import top.hepingan.java.mapper.MuserMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.hepingan.java.mapper.PhoneRegisterMapper;
import top.hepingan.java.service.Impl.service.IMuserService;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-10-28
 */
@Service
public class MuserServiceImpl extends ServiceImpl<MuserMapper, Muser> implements IMuserService {
@Autowired
private PhoneRegisterMapper phoneRegisterMapper;
  public boolean register(String phone)
  {
       LambdaQueryWrapper<PhoneRegister> queryWrapper=
               new LambdaQueryWrapper<>();
       queryWrapper.eq(PhoneRegister::getPhone,phone);
       queryWrapper.eq(PhoneRegister::getStatus,1);
       PhoneRegister phoneRegister = phoneRegisterMapper.selectOne(queryWrapper);
      return phoneRegister != null;

  }
    @Override
    public int register(Muser user) {
        /// 手机号判断
        if (!register(user.getPhone()))
        {
            return -2;
        }
        LambdaQueryWrapper<Muser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Muser::getPhone, user.getPhone()).or().eq(Muser::getNickname, user.getNickname());

        if (this.getOne(queryWrapper)==null){
         boolean save =   this.save(user);
         if (save)
         {
             return 1;
         }

        }
        else
        {
            return -1;
        }

        return 0;
    }

    @Override
    public Muser login(String phone, String password) {
        if (!register(phone))
        {
            return null;
        }
        //手机号登录
        LambdaQueryWrapper<Muser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Muser::getPhone, phone).eq(Muser::getPassword, password);
        return this.getOne(queryWrapper);

    }
}
